package com.videoaggre.zcl.ui.activity.wallpaper.enterdesk

import android.view.Gravity
import android.view.View
import androidx.activity.viewModels
import androidx.fragment.app.commit
import androidx.recyclerview.widget.DefaultItemAnimator
import com.google.android.flexbox.AlignItems
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.videoaggre.zcl.R
import com.videoaggre.zcl.bean.EnterdeskClassify
import com.videoaggre.zcl.helper.InjectBundle
import com.videoaggre.zcl.parse.impl.EnterdeskParse
import com.videoaggre.zcl.ui.activity.BaseActivity
import com.videoaggre.zcl.ui.adapter.EnterdeskTagAdapter
import com.videoaggre.zcl.ui.fragment.enterdesk.EnterdeskClassifyImageFragment
import com.videoaggre.zcl.ui.viewmodel.EnterdeskViewModel
import com.videoaggre.zcl.ui.widget.TitleBarLayout
import kotlinx.android.synthetic.main.enterdesk_activity_brand.*

class EnterdeskBrandImageActivity : BaseActivity(R.layout.enterdesk_activity_brand) {
    private val mEnterdeskViewModel: EnterdeskViewModel by viewModels()
    private val mEnterdeskClassify: EnterdeskClassify by InjectBundle()
    private val mEnterdeskTagAdapter: EnterdeskTagAdapter = EnterdeskTagAdapter()
    override fun initView() {
        bindViewClick(titleView.rightImageView)
        titleView.titleStr = mEnterdeskClassify.name
        titleView.changeStyle(TitleBarLayout.TBL_LOGIN)
        rvBrandList.adapter = mEnterdeskTagAdapter
        rvBrandList.layoutManager = FlexboxLayoutManager(this).apply { flexDirection = FlexDirection.ROW; flexWrap = FlexWrap.WRAP;alignItems = AlignItems.STRETCH }
        (rvBrandList.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
        mEnterdeskTagAdapter.setOnClickAdapterListenter(::onClick)
        supportFragmentManager.commit { replace(R.id.frameLayout, EnterdeskClassifyImageFragment.createInstance(mEnterdeskClassify)) }
        mEnterdeskViewModel.brandDetilLiveData.observe(this) {
            mEnterdeskTagAdapter.datas = EnterdeskParse.parseBrand(it)
            mEnterdeskTagAdapter.notifyDataSetChanged()
        }
    }

    override fun initData() {

    }

    override fun onClick(v: View?) {
        when (v) {
            titleView.rightImageView -> drawerLayout.openDrawer(Gravity.RIGHT)
        }
    }

    private fun onClick(view: View, position: Int) {
        val mEnterdeskClassify = mEnterdeskTagAdapter.getItem(position)
        titleView.titleStr = mEnterdeskClassify.name
        mEnterdeskViewModel.brandDetilChangeLiveData.value = mEnterdeskClassify
        drawerLayout.closeDrawers()
    }
}